package main

import (
	"archive/zip"
	"fmt"
	"io/ioutil"

	jsoniter "github.com/json-iterator/go"
	"github.com/oliveagle/jsonpath"
)

func main() {
	fileName := "/Users/yujinping/data/workspace/mdjk-projects/jxyx-app-testing/doc/购物流程(无购物车).xmind"
	var json_data interface{}
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	content, _ := ReadXMindJsonContent(fileName)
	json.Unmarshal(content, &json_data)
	fmt.Println(string(content))
	fmt.Println("")
	res, err := jsonpath.JsonPathLookup(json_data, "$.title")
	if err == nil {
		fmt.Println("step 1 res: $.title")
		fmt.Println(res)
	}
}

func ReadXMindJsonContent(fileName string) ([]byte, error) {
	empty := make([]byte, 0)
	zr, err := zip.OpenReader(fileName)
	defer zr.Close()
	if err != nil {
		return empty, err
	}

	for _, file := range zr.File {
		fmt.Printf("File: %s\n", file.Name)
		if file.Name == "content.json" {
			fr, err := file.Open()
			if err != nil {
				return empty, err
			}
			content, err := ioutil.ReadAll(fr)
			defer fr.Close()
			if err != nil {
				return empty, err
			}
			return content, nil
		}
	}
	return empty, nil
}
